import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { User } from '../user/user.entity';
import { SongLike } from './song-like.entity';
import { SongCollection } from './song-collection.entity';
import { Category } from '../category/category.entity';
import { Singer } from '../singer/singer.entity';
import { Album } from '../album/album.entity';

@Entity()
export class Song {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @ManyToOne(() => Singer, (singer) => singer.songs)
  artist: Singer;

  @ManyToOne(() => Album, (album) => album.songs, { nullable: true })
  album: Album | null;

  @Column()
  genre: string;

  @Column()
  duration: number; // 以秒为单位

  @Column()
  audioUrl: string;

  @Column({ nullable: true })
  coverImageUrl: string;

  @Column({ nullable: true })
  lyrics: string;

  @Column({ default: 0 })
  plays: number;

  @Column({ default: 0 })
  likes: number;

  @Column({ default: 0 })
  collections: number;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @ManyToOne(() => User, { nullable: true })
  uploadedBy: User;

  @OneToMany(() => SongLike, (songLike) => songLike.song, { cascade: true })
  likesRelations: SongLike[];

  @OneToMany(() => SongCollection, (collection) => collection.song, {
    cascade: true,
  })
  collectionRelations: SongCollection[];

  // 软删除
  @Column({ default: false })
  isDeleted: boolean;

  // 与分类的多对多关系
  @ManyToMany(() => Category)
  @JoinTable()
  categories: Category[];
}
